package com.wheelpicker.picker.addresspicker;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.wheelpicker.R;
import com.wheelpicker.adapters.ArrayWheelAdapter;
import com.wheelpicker.base.WheelType;
import com.wheelpicker.picker.AddressPickerDialog;
import com.wheelpicker.utils.Utils;
import com.wheelpicker.wheel.OnWheelChangedListener;
import com.wheelpicker.wheel.WheelView;

/**
 * Created by jzxiang on 16/4/20.
 */
public class AddressWheel {
    public static final int INDEX_TAB_PROVINCE = 0;//省份标志
    public static final int INDEX_TAB_CITY = 1;//城市标志
    public static final int INDEX_TAB_AREA = 2;//区、县标志
    public static final int INDEX_TAB_STREET = 3;//街道标志
    private int tabIndex = INDEX_TAB_PROVINCE; //默认是省份

    private Context mContext;
    private WheelView province, city, area, street;
    private ArrayWheelAdapter provinceAdapter, cityAdapter, areaAdapter, streetAdapter;
    private AddressPickerConfig mPickerConfig;

    private AddressPickerDialog.AddressProvider addressProvider;

    private OnWheelChangedListener provinceListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            updateCityList();
        }
    };
    private OnWheelChangedListener cityListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            updateAreaList();
        }
    };
    private OnWheelChangedListener areaListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            updateTownList();
        }
    };

    public AddressWheel(View view, AddressPickerConfig pickerConfig) {
        mPickerConfig = pickerConfig;
        addressProvider = pickerConfig.addressProvider;
        mContext = view.getContext();
        initialize(view);
    }

    public void initialize(View view) {
        initView(view);
        initProvince();
    }


    void initView(View view) {
        LinearLayout ll_container = (LinearLayout) view.findViewById(R.id.ll_container);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View child = inflater.inflate(R.layout.layout_address_picker, null, false);
        ll_container.addView(child);
        province = (WheelView) child.findViewById(R.id.province);
        city = (WheelView) child.findViewById(R.id.city);
        area = (WheelView) child.findViewById(R.id.area);
        street = (WheelView) child.findViewById(R.id.street);

        switch (mPickerConfig.mType) {
            case WheelType.AddressType.ALL:

                break;
            case WheelType.AddressType.PROVINCE_CITY_AREA:
                Utils.hideViews(street);
                break;
            case WheelType.AddressType.PROVINCE_CITY:
                Utils.hideViews(area, street);
                break;
            case WheelType.AddressType.PROVINCE:
                Utils.hideViews(city, area, street);
                break;
        }

        province.addChangingListener(provinceListener);
        province.addChangingListener(cityListener);
        province.addChangingListener(areaListener);
        city.addChangingListener(cityListener);
        city.addChangingListener(areaListener);
        area.addChangingListener(areaListener);
    }

    void initProvince() {
        if (province.getVisibility() == View.GONE || addressProvider == null)
            return;
        tabIndex = INDEX_TAB_PROVINCE;
        if (addressProvider.provinceList == null) {
            addressProvider.provinceList = mPickerConfig.addressProvider.getAddressListData(null, "", tabIndex);
        }
        provinceAdapter = new ArrayWheelAdapter<>(mContext, addressProvider.provinceList);
        provinceAdapter.setConfig(mPickerConfig);
        province.setViewAdapter(provinceAdapter);
        province.setCurrentItem(0, false);
        updateCityList();
        updateAreaList();
        updateTownList();
    }


    void updateCityList() {
        if (city.getVisibility() == View.GONE || getCurrentProvince() == null)
            return;
        tabIndex = INDEX_TAB_CITY;
        AddressBean provinceBean = getCurrentProvince();
        addressProvider.cityList = mPickerConfig.addressProvider.getAddressListData(provinceBean, provinceBean.getCode(), tabIndex);
        cityAdapter = new ArrayWheelAdapter(mContext, addressProvider.cityList);
        cityAdapter.setConfig(mPickerConfig);
        city.setViewAdapter(cityAdapter);
        city.setCurrentItem(0, false);
    }

    void updateAreaList() {
        if (area.getVisibility() == View.GONE || getCurrentCity() == null)
            return;
        tabIndex = INDEX_TAB_AREA;
        AddressBean cityBean = getCurrentCity();
        addressProvider.areaList = mPickerConfig.addressProvider.getAddressListData(cityBean, cityBean.getCode(), tabIndex);
        areaAdapter = new ArrayWheelAdapter(mContext, addressProvider.areaList);
        areaAdapter.setConfig(mPickerConfig);
        area.setViewAdapter(areaAdapter);
        area.setCurrentItem(0, false);
    }

    void updateTownList() {
        if (street.getVisibility() == View.GONE || getCurrentArea() == null)
            return;
        tabIndex = INDEX_TAB_STREET;
        AddressBean areaBean = getCurrentArea();
        addressProvider.streetList = mPickerConfig.addressProvider.getAddressListData(areaBean, areaBean.getCode(), tabIndex);
        streetAdapter = new ArrayWheelAdapter(mContext, addressProvider.streetList);
        streetAdapter.setConfig(mPickerConfig);
        street.setViewAdapter(streetAdapter);
        street.setCurrentItem(0, false);
    }


    public AddressBean getCurrentProvince() {
        if (province.getVisibility() == View.GONE || addressProvider == null || addressProvider.provinceList == null)
            return null;
        return addressProvider.provinceList.get(province.getCurrentItem());
    }

    public AddressBean getCurrentCity() {
        if (city.getVisibility() == View.GONE || addressProvider == null || addressProvider.cityList == null)
            return null;
        return addressProvider.cityList.get(city.getCurrentItem());
    }

    public AddressBean getCurrentArea() {
        if (area.getVisibility() == View.GONE || addressProvider == null || addressProvider.areaList == null)
            return null;
        return addressProvider.areaList.get(area.getCurrentItem());
    }

    public AddressBean getCurrentTown() {
        if (street.getVisibility() == View.GONE || addressProvider == null || addressProvider.streetList == null)
            return null;
        return addressProvider.streetList.get(street.getCurrentItem());
    }
}
